﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QueueUnlimited.API;
using System.Configuration;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
using System.Web.UI;

namespace MultiplayerDice.Implementation
{
    public static class EventRaiser
    {
        public static void Raise<T>(T businessEvent)
        {
            using (var dataStore = new DataStore(ConfigurationManager.ConnectionStrings["MultiplayerDiceEventBusConnectionString"].ConnectionString))
            {
                dataStore.Enqueue(typeof(T).FullName, Serialize(businessEvent));
            }
        }

        private static string Serialize<T>(T businessEvent)
        {
            var serializer = new LosFormatter();
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(stream, businessEvent);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                    return reader.ReadToEnd();
            }
        }
    }
}
